<?php
namespace Shopmodule\Model;
use Think\Model;
class SeedsModel extends Model{
	public $usernick;
	public $uzuid;
	public $sid;
	public $limit;
	public function qiandao(){
		$User = D("ShopUsersView");
		$seed =  current($User->where("`usernick`='".$this->usernick."'")->getField("userid,id,userid,score,addtime,lastlogin,lastseeds,nextseeds"));
		if(!$seed){
			$U = D("User");
			$U->usernick=$this->usernick;
			$U->sid=$this->sid;
			$U->getShopUser();
			$seed['id']=$U->suid;
			$seed['userid']=$U->uzuid;
			$seed['lastseeds']=0;
			$seed['nextseeds']=0;
			$seed['usernic']=$this->usernick;
			// $seed =  current($User->where("`usernick`='".$this->usernick."'")->getField("userid,id,userid,score,addtime,lastlogin,lastseeds,nextseeds"));
		}
		$userseeds = $this->getUserSeeds();
		// $userseeds=C("GET_SEEDS");
		// $lottsseeds=C("GET_SEEDS_LOTTS");
		if(date("Ymd",strtotime($seed["lastseeds"]))==date("Ymd",time())&&$seed["lastseeds"]){
			$info["status"]=false;
			$info["msg"]="今天您已签过到，请明天再来！";
		}else{
			$point = $this->getPoint($seed,$userseeds);
			$morepoint = $this->getMorePoint($seed);
			$data["seeds"]=array("exp","seeds + ".($point+$morepoint));
			$data["lastseeds"]=date("Y-m-d H:i:s",time());
			$data["nextseeds"]=$point;
			$EU = M("ShopUsers");
			$EU->where("`id`=".$seed["id"])->save($data);
			$nextseeds = intval($userseeds[$point]);
			// $User->execute("update `uz_status` set `all_seeds` = `all_seeds`+".($point+$morepoint));
			
			$Seeds = M("Seeds");
			$da["uzuid"]=$seed["userid"];
			$da["quangity"]=$point+$morepoint;
			$da["addtime"]=date("Y-m-d H:i:s",time());
			$da["addby"]=1;
			$da["type"]=1;
			$da["reason"]=1;
			$da["status"]=1;
			$da["sid"]=$this->sid;
			$Seeds->add($da);
			
			$info["status"]=true;
			$info["point"]=$poin+$morepoint;
			$info["msg"]="您累计抽奖".$seed["lotts"]."次，获得".$morepoint."的签到加成，已领取".($point+$morepoint)."颗种子。明天签到可获得".($userseeds[$point]+$morepoint)."种子！";
		}
		return $info;
	}
	private function getUserSeeds(){
		$CFG = M("ToolsStatus");
		$cfg = json_decode($CFG->where("`tool_id`=13 AND `sid`=".$this->sid)->getField("cfg"),true);
		$i=0;
		foreach($cfg["day"] as $k=>$key){
			if($k==0){
				$userseeds[0] = $key; 
			}
			$userseeds[$key] = $cfg["day"][$k+1]; 
			$i++;
		}
		$userseeds[$cfg["day"][$i-1]]=$cfg["day"][$i-1];
		return $userseeds;
	}
	private function getPoint($seed,$userseeds){
		$Yesterday = date('Ymd',time()-3600*24);
		if($Yesterday==date("Ymd",strtotime($seed['lastseeds']))){
			$point = intval($userseeds[$seed['nextseeds']]); 
		}else{
			$point = intval($userseeds[0]); 
		}
		if(empty($point)){
			$point = intval($userseeds[0]); 
		}
		return $point;
	}
	private function getMorePoint($seed){
		$lottsseeds=C("GET_SEEDS_LOTTS");
		$morepoint = 0;
		$next=false;
		if($seed["lotts"]>99){
			foreach($lottsseeds as $k=>$s){
				if($seed["lotts"]>$k){
					$morepoint = $s;
				}
				if($next){
					$this->nextlotts=$k;
					$this->nextpoint=$s;
					$next=false;
				}
				if($morepoint == $s){
					$next=true;
				}
				
			}
		}else{
			$this->nextlotts=100;
			$this->nextpoint=10;
		}
		return $morepoint;
	}

}